package com.naodong.shenluntiku.mvp.view.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.naodong.shenluntiku.mvp.model.bean.Assessment;
import com.naodong.shenluntiku.mvp.view.fragment.AssessmentLastPagerFragment;
import com.naodong.shenluntiku.mvp.view.fragment.SubjectInfoFragment;

import java.util.HashMap;

/**
 * Created by hujie on 17/5/10.
 */

public class AssessmentFragmentAdapter extends PagerAdapter {


    private FragmentTransaction mCurTransaction = null;
    private Fragment mCurrentPrimaryItem = null;
    private HashMap<Integer, Fragment> fragments;
    private FragmentManager fragmentManager;
    private Assessment assessment;

    int lastPagerMode = 1;

    public AssessmentFragmentAdapter(FragmentManager fm, Assessment assessment, int lastPagerMode) {
        fragmentManager = fm;
        this.assessment = assessment;
        this.lastPagerMode = lastPagerMode;
        fragments = new HashMap<>();
    }

    /**
     * 更新最後一個
     * @param lastPagerMode
     */
    public void updateLastPagerMode(int lastPagerMode) {
        this.lastPagerMode = lastPagerMode;
        Fragment fragment = fragmentManager.findFragmentByTag(makeFragmentName(getCount()-1));
        if(fragment!=null&&fragment instanceof AssessmentLastPagerFragment){
            ((AssessmentLastPagerFragment) fragment).updateLastPagerMode(lastPagerMode);
        }
    }

    public Fragment getItem(int position) {
        if (fragments.containsKey(getItemId(position))) {
            return fragments.get(getItemId(position));
        }
        Fragment fragment = null;
        if (position == getCount() - 1) {
             fragment = AssessmentLastPagerFragment.getInstance(lastPagerMode,assessment);
        } else {
            fragment = SubjectInfoFragment.getInstance(assessment.isFinish(), assessment.getSubjects().get(position));
            fragments.put(getItemId(position), fragment);
        }
        return fragment;
    }


    @Override
    public int getCount() {
        return assessment.getAssessNum() + 1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (fragments.size() > position) {
            Fragment fragment = fragments.get(position);
            fragmentManager.beginTransaction().hide(fragment).commit();
        }
    }

    @Override
    public void startUpdate(ViewGroup container) {
        if (container.getId() == View.NO_ID) {
            throw new IllegalStateException("ViewPager with adapter " + this
                    + " requires a view id");
        }
    }




    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = fragmentManager.beginTransaction();
        }

        Fragment fragment = getItem(position);
        if (!fragment.isAdded()) {
            mCurTransaction.add(container.getId(), fragment,
                    makeFragmentName(position));
        } else {
            mCurTransaction.show(fragment).commitNow();
        }
        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }
        return fragment;
    }


    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment) object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setMenuVisibility(false);
                mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            mCurrentPrimaryItem = fragment;
        }
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        if (mCurTransaction != null) {
            mCurTransaction.commitNowAllowingStateLoss();
            mCurTransaction = null;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment) object).getView() == view;
    }


    /**
     * Return a unique identifier for the item at the given position.
     *
     * <p>The default implementation returns the given position.
     * Subclasses should override this method if the positions of items can change.</p>
     *
     * @param position Position within this adapter
     * @return Unique identifier for the item at position
     */
    public Integer getItemId(int position) {
        return position;
    }

    private static String makeFragmentName( int postion) {
        return "fragment:" + postion;
    }


}
